/**
 * 
 */
package com.aspire.wap.core.log;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author x_huyunan 2007-9-12 上午09:48:14
 * @version 1.0
 * @deprecated
 */

public class WapLogger {

    private Log logger = null;

    /**
     * 私有构造函数
     * 
     * @param logger
     */
    private WapLogger(Log logger)
    {
        this.logger = logger;
    }

    /**
     * <p/> All <code>WapLogger</code> callers are assumed to get a WapLogger
     * instance by this method instead of the traditional constructor's way.
     * 
     * @author x_huyunan 2007-9-12 上午09:55:20
     * @version 1.0
     * @param clazz
     *            caller class
     * @return WapLogger instance
     */
    public static WapLogger getLogger(Class clazz)
    {
        Log logger = LogFactory
                .getLog((clazz == null) ? null : clazz.getName());
        return new WapLogger(logger);
    }

    /**
     * <p/> All <code>WapLogger</code> callers are assumed to get a WapLogger
     * instance by this method instead of the traditional constructor's way.
     * 
     * @author x_huyunan 2007-9-12 上午09:58:35
     * @version 1.0
     * @param name
     *            logger name
     * @return WapLogger instance
     */
    public static WapLogger getLogger(String name)
    {
        Log logger = LogFactory.getLog((name == null) ? null : name);
        return new WapLogger(logger);
    }

    // ******* 根据WAP组日志使用规范定义的方法 ******//

    /**
     * Debug等级的log输出
     * 
     * @author x_huyunan 2007-9-13 上午09:49:29
     * @version 1.0
     * @param message
     *            log信息
     */
    public void wapDebug(String message)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug(message);
        }
    }

    // ******* 针对字符数组进行输出 ******//

    /**
     * Debug等级的log输出
     * 
     * @author x_huyunan 2007-9-13 上午09:49:29
     * @version 1.0
     * @param property
     *            属性数组
     * @param value
     *            值数组
     */
    public void wapDebug(String[] property, String[] value)
    {
        // 判断debug等级是否允许
        if (logger.isDebugEnabled())
        {
            logger.debug(formLogMessage(property, value));
        }
    }

    /**
     * Info等级的log输出
     * 
     * @author x_huyunan 2007-9-13 上午09:53:17
     * @version 1.0
     * @param property
     *            属性数组
     * @param value
     *            值数组
     */
    public void wapInfo(String[] property, String[] value)
    {
        logger.info(formLogMessage(property, value));
    }

    /**
     * Error等级的log输出
     * 
     * @author x_huyunan 2007-9-13 上午09:54:05
     * @version 1.0
     * @param property
     *            属性数组
     * @param value
     *            值数组
     */
    public void wapError(String[] property, String[] value)
    {
        logger.error(formLogMessage(property, value));
    }

    /**
     * Level等级的log输出
     * 
     * @author x_huyunan 2007-9-13 上午09:55:37
     * @version 1.0
     * @param level
     * @param property
     *            属性数组
     * @param value
     *            值数组
     */
    public void wapLog(Level level, String[] property, String[] value)
    {
        log(level, formLogMessage(property, value));
    }

    /**
     * 根据规范构造log信息
     * 
     * @author x_huyunan 2007-9-13 上午09:48:55
     * @version 1.0
     * @param property
     *            属性数组
     * @param value
     *            值数组
     * @return log信息
     */
    private String formLogMessage(String[] property, String[] value)
    {
        String logMessage = "";
        if (property == null || property.length == 0 || value == null
                || value.length == 0)
        {
            logMessage = "[]";
        }
        else
        {
            StringBuilder builder = new StringBuilder();
            // 循环根据长度短的进行
            int loop = (property.length <= value.length) ? property.length
                    : value.length;
            builder.append("[").append(property[0]).append("=")
                    .append(value[0]);
            for (int i = 1; i < loop; i++)
            {
                builder.append(",").append(property[i]).append("=").append(
                        value[i]);
            }
            builder.append("]");
            logMessage = builder.toString();
        }
        return logMessage;
    }

    // ******* 针对List进行输出 ******//

    /**
     * 根据规范构造log信息
     * 
     * @author x_huyunan 2007-9-13 上午10:03:53
     * @version 1.0
     * @param property
     *            属性
     * @param value
     *            属性值
     * @return log信息
     */
    private String formLogMessage(List<String> property, List<String> value)
    {
        String logMessage = "";
        if (property == null || property.isEmpty() || value == null
                || value.isEmpty())
        {
            logMessage = "[]";
        }
        else
        {
            StringBuilder builder = new StringBuilder();
            // 循环根据长度短的进行
            int loop = (property.size() <= value.size()) ? property.size()
                    : value.size();
            builder.append("[").append(property.get(0)).append("=").append(
                    value.get(0));
            for (int i = 1; i < loop; i++)
            {
                builder.append(",").append(property.get(i)).append("=").append(
                        value.get(i));
            }
            builder.append("]");
            logMessage = builder.toString();
        }
        return logMessage;
    }

    /**
     * Debug等级的log输出
     * 
     * @author x_huyunan 2007-9-13 上午10:07:55
     * @version 1.0
     * @param property
     *            属性
     * @param value
     *            属性值
     */
    public void wapDebug(List<String> property, List<String> value)
    {
        // 判断debug等级是否允许
        if (logger.isDebugEnabled())
        {
            logger.debug(formLogMessage(property, value));
        }
    }

    /**
     * Info等级的log输出
     * 
     * @author x_huyunan 2007-9-13 上午10:07:55
     * @version 1.0
     * @param property
     *            属性
     * @param value
     *            属性值
     */
    public void wapInfo(List<String> property, List<String> value)
    {
        logger.info(formLogMessage(property, value));
    }

    /**
     * Error等级的log输出
     * 
     * @author x_huyunan 2007-9-13 上午10:07:55
     * @version 1.0
     * @param property
     *            属性
     * @param value
     *            属性值
     */
    public void wapError(List<String> property, List<String> value)
    {
        logger.error(formLogMessage(property, value));
    }

    /**
     * Level等级的log输出
     * 
     * @author x_huyunan 2007-9-13 上午10:07:55
     * @version 1.0
     * @param property
     *            属性
     * @param value
     *            属性值
     */
    public void wapLevel(Level level, List<String> property, List<String> value)
    {
        log(level, formLogMessage(property, value));
    }

    // ******* 针对HashMap进行输出 ******//

    /**
     * 根据规范构造log信息
     * 
     * @author x_huyunan 2007-9-13 上午11:05:21
     * @version 1.0
     * @param map
     *            信息Map
     * @return log信息
     */
    private String formLogMessage(HashMap<String, String> map)
    {
        String logMessage = "";
        if (map == null || map.isEmpty())
        {
            logMessage = "[]";
        }
        else
        {
            StringBuilder builder = new StringBuilder();
            builder.append("[");
            Iterator<String> keys = map.keySet().iterator();
            String property = "";
            String value = "";
            while (keys.hasNext())
            {
                property = keys.next();
                if (StringUtils.isNotEmpty(property))
                {
                    value = map.get(property);
                    if (StringUtils.isNotEmpty(value))
                    {
                        builder.append(property).append("=").append(value)
                                .append(",");
                    }
                }
            }
            // 如果最后一位是,则删除
            if (builder.lastIndexOf(",") == builder.length() - 1)
            {
                builder.deleteCharAt(builder.lastIndexOf(","));
            }
            builder.append("]");
            logMessage = builder.toString();
        }
        return logMessage;
    }

    /**
     * Debug等级的log输出
     * 
     * @author x_huyunan 2007-9-13 上午11:07:22
     * @version 1.0
     * @param map
     *            信息Map
     */
    public void wapDebug(HashMap<String, String> map)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug(formLogMessage(map));
        }
    }

    /**
     * Info等级的log输出
     * 
     * @author x_huyunan 2007-9-13 上午11:07:22
     * @version 1.0
     * @param map
     *            信息Map
     */
    public void wapInfo(HashMap<String, String> map)
    {
        logger.info(formLogMessage(map));
    }

    /**
     * Error等级的log输出
     * 
     * @author x_huyunan 2007-9-13 上午11:07:22
     * @version 1.0
     * @param map
     *            信息Map
     */
    public void wapError(HashMap<String, String> map)
    {
        logger.error(formLogMessage(map));
    }

    /**
     * Level等级的log输出
     * 
     * @author x_huyunan 2007-9-13 上午11:07:22
     * @version 1.0
     * @param map
     *            信息Map
     */
    public void wapLevel(Level level, HashMap<String, String> map)
    {
        log(level, formLogMessage(map));
    }

    // ******* 针对Bean进行输出 ******//

    private String formLogMessage(Object bean)
    {
        String logMessage = "";
        if (bean == null)
        {
            logMessage = "";
        }
        else
        {
            StringBuilder builder = new StringBuilder();
            builder.append("[");
            Map<String, String> map;
            try
            {
                map = BeanUtils.describe(bean);
                Iterator<String> keys = map.keySet().iterator();
                String property = "";
                String value = "";
                while (keys.hasNext())
                {
                    property = keys.next();
                    if (StringUtils.isNotEmpty(property))
                    {
                        value = map.get(property);
                        if (StringUtils.isNotEmpty(value))
                        {
                            builder.append(property).append("=").append(value)
                                    .append(",");
                        }
                    }
                }
                // 如果最后一位是,则删除
                if (builder.lastIndexOf(",") == builder.length() - 1)
                {
                    builder.deleteCharAt(builder.lastIndexOf(","));
                }
            }
            catch (Exception ex)
            {
                // ex.printStackTrace();
            }
            builder.append("]");
            logMessage = builder.toString();
        }
        return logMessage;
    }

    /**
     * Debug等级的log输出
     * 
     * @author x_huyunan 2007-9-13 下午01:57:21
     * @version 1.0
     * @param bean
     */
    public void wapDebug(Object bean)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug(formLogMessage(bean));
        }
    }

    /**
     * Info等级的log输出
     * 
     * @author x_huyunan 2007-9-13 下午01:57:21
     * @version 1.0
     * @param bean
     */
    public void wapInfo(Object bean)
    {
        logger.info(formLogMessage(bean));
    }

    /**
     * Error等级的log输出
     * 
     * @author x_huyunan 2007-9-13 下午01:57:21
     * @version 1.0
     * @param bean
     */
    public void wapError(Object bean)
    {
        logger.error(formLogMessage(bean));
    }

    /**
     * Level等级的log输出
     * 
     * @author x_huyunan 2007-9-13 下午01:57:21
     * @version 1.0
     * @param bean
     */
    public void wapLevel(Level level, Object bean)
    {
        log(level, formLogMessage(bean));
    }

    // ******* 通用方法 ******//

    /**
     * Log a message object with the DEBUG level. To print a stack trace use the
     * {@link #logDebug(Object,Throwable)} form instead.
     * 
     * @param message
     *            the message object to log.
     */
    public void logDebug(Object message)
    {
        logger.debug(message);
    }

    /**
     * Log a message object with the DEBUG level including the stack trace of
     * the <code>Throwable t</code> passed as parameter
     * 
     * @param message
     *            the message object to log.
     * @param t
     *            the exception to log, including its stack trace.
     */
    public void logDebug(Object message, Throwable t)
    {
        logger.debug(message, t);
    }

    /**
     * Log a message object with the ERROR level. To print a stack trace use the
     * {@link #logError(Object,Throwable)} form instead.
     * 
     * @param message
     *            the message object to log.
     */
    public void logError(Object message)
    {
        logger.error(message);
    }

    /**
     * Log a message object with the ERROR level including the stack trace of
     * the <code>Throwable t</code> passed as parameter
     * 
     * @param message
     *            the message object to log.
     * @param t
     *            the exception to log, including its stack trace.
     */
    public void logError(Object message, Throwable t)
    {
        logger.error(message, t);
    }

    /**
     * Log a message object with the FATAL level.
     * 
     * @param message
     *            the message object to log.
     */
    public void logFatal(Object message)
    {
        logger.fatal(message);
    }

    /**
     * Log a message object with the FATAL level including the stack trace of
     * the <code>Throwable t</code> passed as parameter
     * 
     * @param message
     *            the message object to log.
     * @param t
     *            the exception to log, including its stack trace.
     */
    public void logFatal(Object message, Throwable t)
    {
        logger.fatal(message, t);
    }

    /**
     * Log a message object with the WARN level.
     * 
     * @param message
     *            the message object to log.
     */
    public void logWarning(Object message)
    {
        logger.warn(message);
    }

    /**
     * Log a message object with the WARN level including the stack trace of the
     * <code>Throwable t</code> passed as parameter
     * 
     * @param message
     *            the message object to log.
     * @param t
     *            the exception to log, including its stack trace.
     */
    public void logWarning(Object message, Throwable t)
    {
        logger.warn(message, t);
    }

    /**
     * Log a message object with the INFO level.
     * 
     * @param message
     *            the message object to log.
     */
    public void logInfo(Object message)
    {
        logger.info(message);
    }

    /**
     * Log a message object with the INFO level including the stack trace of the
     * <code>Throwable t</code> passed as parameter
     * 
     * @param message
     *            the message object to log.
     * @param t
     *            the exception to log, including its stack trace.
     */
    public void logInfo(Object message, Throwable t)
    {
        logger.info(message, t);
    }

    /**
     * Log a message object with the Trace level.
     * 
     * @param message
     *            the message object to log.
     */
    public void logTrace(Object message)
    {
        logger.trace(message);
    }

    /**
     * Log a message object with the Trace level including the stack trace of
     * the <code>Throwable t</code> passed as parameter
     * 
     * @param message
     *            the message object to log.
     * @param t
     *            the exception to log, including its stack trace.
     */
    public void logTrace(Object message, Throwable t)
    {
        logger.trace(message, t);
    }

    /**
     * 输出log
     * 
     * @author x_huyunan 2007-9-12 下午05:58:07
     * @version 1.0
     * @param level
     *            log等级
     * @param message
     *            log信息
     */
    public void log(Level level, Object message)
    {
        log(level, message, null);
    }

    /**
     * 输出log
     * 
     * @author x_huyunan 2007-9-12 下午05:57:00
     * @version 1.0
     * @param level
     *            log等级
     * @param message
     *            log信息
     * @param t
     *            异常
     */
    public void log(Level level, Object message, Throwable t)
    {
        if (level.level == Level.DEBUG_INT)
        {
            logger.debug(message, t);
        }
        else if (level.level == Level.INFO_INT)
        {
            logger.info(message, t);
        }
        else if (level.level == Level.WARN_INT)
        {
            logger.warn(message, t);
        }
        else if (level.level == Level.ERROR_INT)
        {
            logger.error(message, t);
        }
        else if (level.level == Level.FATAL_INT)
        {
            logger.fatal(message, t);
        }
        else if (level.level == Level.TRACE_INT)
        {
            logger.trace(message, t);
        }
    }

    /**
     * 按照theMessage中定义的模式将数组中对应的赋值。 例如theMessage为abc{0}def{1}则会将数组中对应的值赋到相应的位置.
     * 如果没有相应的值则赋null，超出数组范围不赋值。
     * 
     * @param level
     *            log等级
     * @param theMessage
     *            log信息
     * @param objArray
     *            赋值内容
     */
    public void logArray(Level level, String theMessage, Object[] objArray)
    {
        if (theMessage == null)
        {
            return;
        }
        log(level, MessageFormat.format(theMessage, objArray));
    }

    /**
     * 判断log等级是否支持
     * 
     * @param level
     *            log等级
     * @return boolean support the level will return true, otherwise return
     *         false.
     */
    public boolean isEnabled(Level level)
    {
        if (level.level == Level.DEBUG_INT)
        {
            return logger.isDebugEnabled();
        }
        else if (level.level == Level.INFO_INT)
        {
            return logger.isInfoEnabled();
        }
        else if (level.level == Level.WARN_INT)
        {
            return logger.isWarnEnabled();
        }
        else if (level.level == Level.ERROR_INT)
        {
            return logger.isErrorEnabled();
        }
        else if (level.level == Level.FATAL_INT)
        {
            return logger.isFatalEnabled();
        }
        else if (level.level == Level.TRACE_INT)
        {
            return logger.isTraceEnabled();
        }
        else
        {
            return false;
        }
    }

}